<?php

/*
 * Class used to extend the base product class
 * Created 3rd May, 2012
 * Created by: Integration Team
 */

/**
 * This class adds functionality for a custom implementation 
 * of: 
 * Packaged contents
 * Overview
 * Technical Specs
 * 
 * With custom Markup 
 * 
 */
class OmegaProduct extends product
{
   
    private $packaged_contents = "";
    
   public function get_package_contents()
   {
       $this->packaged_contents = $this->get_attribute(34172);
        if($this->packaged_contents=="")
        {
            return "";
        }
        else
        {
            $values = explode("<li>", $this->packaged_contents);
            $marked_up_values = "";
            
                foreach ($values as $key=>$value)
                {
                    if($value <> "")
                    {
                        $marked_up_values.="<ul><li><span>".str_replace("</li>", "", $value)."</span></li></ul>";
                    }
                }
            return $marked_up_values;    
        
        }
    }


    public function get_mapped_category($categoryId=0)
    {
        //Returns the corresponding mapped categoryId
        //@param - The Etilize categoryId for which the mapped_category(s) need to be returned
        if($categoryId == 0)
        {
            return "";

        }
        $query = "Select group_concat(custom_categoryid) as categories
                from ".Custom_DB_name.".category_mapping where et_categoryid=".$categoryId." group by et_categoryid";
        $result = query($query);

        if(mysql_num_rows($result)> 0)
        {
            $categories = mysql_fetch_assoc($result);
            return $categories['categories'];
        }

    }
    
    //Over-ride the format_techspecs and format_overview in order
    //to customize for Omega
    protected function format_techspecs()
    {
        //sets the formatting for the technical specs, assigns the attribute values
        //to the attribute_data variable and converts 
        //the productInformation array into a string (This is useful in order to avoid
        //the productInformation array from hogging memory)
        
        echo "I was called from the sub-class";
            
        if (!isset($this->productInformation['attributes'])) 
        {
            return "";
        }
        
        
        if ($this->templateId == 1) {
            $attribute_set = $this->productInformation['attributes'][1];
        } else {
            $attribute_set = $this->productInformation['attributes'][0];
        }
        if(!isset($attribute_set))
        {
            return "";
        }
        else
        {
            
            foreach ($attribute_set as $headerId => $headerName) 
            {
                foreach ($headerName as $headerName => $attributes) 
                {
                    //$techSpecs.= "<div style=\"font-size:12px;margin:5px 0 5px 0\"><b>" . $headerName . "</b><hr>";
                        foreach ($attributes as $attributeId => $attribute) 
                        {
                            //Marketing information, Product Name and Manufacturer Website Address don't need to be added 
                                foreach ($attribute as $attributeName => $value) 
                                {
                                    if($attributeName<>'Marketing Information' && $attributeName<>'Product Name' && $attributeName<>'Manufacturer Website Address')
                                    {    
                                        if (count($value) == 1) 
                                        {
                                            $techSpecs.= "<tr><td style=\"border: 1px solid black;\">" . $attributeName . "</td><td style=\"border: 1px solid black;\">" . $this->strip_html_tags($value[0]) . "</td></tr>";
                                        } 
                                        else 
                                        {
                                            $techSpecs.= "<tr><td style=\"border: 1px solid black;\">" . $attributeName . "</td><td style=\"border: 1px solid black;\">";
                                            foreach ($value as $value) 
                                            {
                                                $techSpecs.= "<li>" . $this->strip_html_tags($value) . "</li>";
                                            }
                                            $techSpecs.="</td></tr>";
                                        }
                                    }
                                }
                        }
                }
                
            }
            if($techSpecs<>"")
            {
                $this->techspecs = "<table style=\"border: 1px solid black; width:100%;\" class=\"table-specifications\" cellpadding=\"10\" cellspacing=\"1\" width=\"\" align=\"left\"><tbody><tr><td style=\"border: 1px solid black;\" colspan=\"2\"><span>SPECIFICATIONS</span></td>
        </tr>".$techSpecs."</tbody></table>";
            }
            else
            {
                return "";
            }
            
        }
    }
    
    protected function format_overview() {
     //over-ride the base product formatting 
     // in order to customize the formatting for Omega
        
     $feature_set = $this->get_attribute(3411477);
        if($feature_set<>"")
            
          //If Product Features exist then use it - otherwise create the main overview  
        {
            
            $values = explode("<li>", $feature_set);
            
                foreach($values as $key=>$value)
                {
                    if($value<>"")
                    {
                        $marked_up_values .= "<ul><li><span>".$this->strip_html_tags($value)."</span></li></ul>";
                    }
                }
            if($marked_up_values<>"")
            {
                $this->overview = "<div class=\"list-holder\"><span></span>".$marked_up_values."</div>";
                return true;
            }
            else
            {
                return false;
            }
        }
        else 
        {
            
            if (!isset($this->productInformation['attributes'][0])) 
            {
            
            return "";
            }

            $marked_up_values = "<div class=\"list-holder\"><span></span>";
        
                
            foreach ($this->productInformation['attributes'][0] as $headerId => $headerName) 
            {
                //Only the basic set is needed for the overview 

                if ($headerId <> 35) 
                {
                    foreach ($headerName as $headerName => $attributes) 
                    {

                        foreach ($attributes as $attributeId => $attributeName) 
                        {
                            //if the attribute is Marketing Text
                            
                                foreach($attributeName as $attributeName => $value)
                                {
                                    //Marketing Text, Product Name and Manufacturer Website Address don't need to be added
                                    if($attributeName<>'Marketing Information' && $attributeName<>'Product Name' && $attributeName<>'Product Name')
                                    {
                                        if (count($value) == 1) 
                                        {
                                        
                                        $marked_up_values.= "<ul><li><span>" . $attributeName . ":  " . $this->strip_html_tags($value[0]) . "</span></li></ul>";
                                        } 
                                        else 
                                        {
                                        $marked_up_values.= "<ul><li><span>" . $attributeName . ": " . $this->strip_html_tags(implode(', ', $value)) . "</span></li></ul>";
                                        }
                                    }
                                    

                                }

                        }
                    }
                } 

            }
            if($marked_up_values<>"")
            {
                $this->overview = "<div class=\"list-holder\"><span></span>".$marked_up_values."</div>";
                return true;
            }
            else
            {
                return false;
            }
        }
    }
    
    
    
    public function strip_html_tags($string = "")
    {
        $string = str_replace(array("<ul>", "</ul>", "<li>", "</li>"), '', $string);
        return $string;
    }
}
?>
